/**
 * Created by sue on 2017/6/20.
 */
layer.confirm('请确保提交成功，返回之后数据会清空，确定返回吗？', {
    btn: ['返回','继续'] //按钮
}, function(){

    if(pushId){
        //alert('关闭窗口');
        window.close();
    }else{
        var url = window.location.origin+window.location.pathname+'#/ad/resource';
        location.href = url;
        layer.msg('返回成功');
    }
}, function(){

});





$http({
    method: 'POST',
    url: window.reqUrl+'/management/push/insertOrUpdate',
    data: $scope.data,
    transformRequest: function (obj) {
        var str = [];
        for (var p in obj) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
        return str.join("&");
    },
    headers: {'Content-Type': 'application/x-www-form-urlencoded','authorization':token},
}).success(function (response) {
    if (response.result == true) {
        if (!authorId) {
            layer.msg('提交成功!');
            setTimeout(function(){
                var url = window.location.origin + window.location.pathname +'#/writer';
                window.location.href = url;
            },1500)
        } else {
            layer.msg('修改成功!')
            setTimeout(function(){
                window.close();
            },1500)
        }
    }else{
        layer.msg(response.message);
    }
}).error(function (response) {
    layer.msg(response.message);
});


$('#showPageName').text('新增资源主推......O(∩_∩)O~~');



if(link){
    //第一张图片的删除
    layer.confirm('确定删除图片吗？<br><img src="'+link+'" alt="">', {
        btn: ['确定','取消'] //按钮
    }, function(){
        //判断是删的哪张
        $timeout(function(){
            $scope.data.icon = '';
        },0);
        layer.msg('删除成功', {icon: 1});
    }, function(){

    });
}


layer.confirm(status==1?'确定显示吗？':'确定隐藏吗?', {
    btn: ['确定','取消'] //按钮
}, function(){
    if(status == 1){
        showOrHide(0, id, $scope);
    }else{
        showOrHide(1, id, $scope);
    }

}, function(){

});

if($scope.listArr[$scope.$index].status == 0){
    $scope.listArr[$scope.$index].status = 1;
}else{
    $scope.listArr[$scope.$index].status = 0;
}
layer.msg('操作成功!');



$('#adImg_add_mold option[value="' + res.data.data.mold + '"]').attr('selected', 'selected');
$('#adImg_releaseTime').val(res.data.data.releaseTime)
$("input[name='mold_type'][value='" + res.data.data.type + "']").attr('checked', true);
$("input[name='addType'][value='" + res.data.data.status + "']").attr('checked', true);


$scope.dataAll.mold = parseInt($('#adImg_add_mold option:checked').val());
$scope.dataAll.type = parseInt($('input:radio[name=mold_type]:checked').val());
$scope.dataAll.releaseTime = $('#adImg_releaseTime').val();
$scope.dataAll.status = $('input:radio[name=addType]:checked').val();


//图片上传
$('#upload_adImg_icon').on('change', function (e) {
    var file = e.target.files[0];
    var endName = file.name.split('.')[1];
    var storeAs = Date.parse(new Date()) + '.' + endName;
    client.multipartUpload(storeAs, file).then(function (result) {
        var iconUrl = result.res.requestUrls[0].split('?');
        $timeout(function(){
            $scope.dataAll.icon = iconUrl[0].replace('http', 'https');
        },0)
    }).catch(function (err) {
        console.log(err);
    });
    $(this).val('');
});


$('#article_add_audio').on('change', function (e) {
    var file = e.target.files[0];
    var endName = file.name.split('.')[1];
    var storeAs = Date.parse(new Date()) + '.' + endName;
    $('#audio_col_file span.course').text('正在上传...');
    client.multipartUpload(storeAs, file).then(function (result) {
        var audioUrl = result.res.requestUrls[0].split('?');
        if (audioUrl.length > 0) {
            $('.delAudioFile').show();
            layer.msg('上传成功!');
        }
        $timeout(function(){
            $scope.data.audioLink = audioUrl[0].replace('http', 'https');
        },0);
    }).catch(function (err) {
        console.log(err);
    });
    $(this).val('');
});
